package com.example.crmweb.controller;


import com.example.crmweb.entity.Dictionary;
import com.example.crmweb.lang.GlobalExceptionHandler;
import com.example.crmweb.lang.Result;
import com.example.crmweb.service.DictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author -CRM-
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 查询所有
     * @return list
     */
    @GetMapping("/batchGet")
    public Result batchDescribe(
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum,
            Dictionary dictionary){
        PageInfo<Dictionary> dictionaryPageInfo = dictionaryService.batchDescribeByPage(pageSize, pageNum, dictionary);
        return Result.success(dictionaryPageInfo);
    }

    /**
     * 根据类型查询
     * @param type 类型
     * @return list
     */
    @GetMapping("/search")
    public Result describeById(String type){
        List<Dictionary> dictionaries = dictionaryService.batchDescribeByType(type);
        return Result.success(dictionaries);
    }

    /**
     * 根据状态查询
     * @param status 状态
     * @return list
     */
    @GetMapping("/status")
    public Result batchDescribeByStatus(String status) {
        List<Dictionary> dictionaries = dictionaryService.batchDescribeByStatus(status);
        return Result.success(dictionaries);
    }

    /**
     * 根据类型查询正常的
     * @param type 类型
     * @return list
     */
    @GetMapping("/access")
    public Result batchDescribeByTypeAndAccess(String type) {
        List<Dictionary> dictionaries = dictionaryService.batchDescribeByTypeAndAccess(type);
        return Result.success(dictionaries);
    }

    /**
     * 根据类型查询禁用的
     * @param type 类型
     * @return list
     */
    @GetMapping("/disable")
    public Result batchDescribeByTypeAndDisable(String type) {
        List<Dictionary> dictionaries = dictionaryService.batchDescribeByTypeAndDisable(type);
        return Result.success(dictionaries);
    }

    /**
     * 根据id查询
     * @param id id
     * @return dictionary
     */
    @GetMapping("/{id}")
    public Result describe(@PathVariable("id") Integer id){
        try{
            Optional<Dictionary> optionalDictionary = dictionaryService.describeById(id);
            return Result.success(optionalDictionary);
        }catch (Exception e){
            return Result.fail(500,"没有这个分类",null);
        }

    }

    /**
     * 新增
     * @param dictionary dictionary
     * @return int
     */
    @PostMapping("")
    public Result create(Dictionary dictionary){
        int insert = dictionaryService.create(dictionary);
        if (insert == 1){
            return Result.success(200,"添加成功",null);
        }else {
            return Result.fail("添加失败，请稍后再试");
        }
    }

    /**
     * 修改
     * @param dictionary
     * @return int
     */
    @PutMapping("")
    public Result modify(Dictionary dictionary){
        Integer modify = dictionaryService.modify(dictionary);
        if (modify == 1){
            return Result.success(200,"修改成功",null);
        }else {
            return Result.fail("修改失败，请稍后再试");
        }
    }

    /**
     * 禁用
     * @param id
     * @return int
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id){
        try{
            Integer delete = dictionaryService.delete(id);
            if (delete == 1){
                return Result.success(200,"删除成功",null);
            }else {
                return Result.fail("删除失败，请稍后再试");
            }
        }catch (Exception e){
            return Result.fail("没有这个分类");
        }

    }

    /**
     * 批量禁用
     * @param ids
     * @return result
     */
    @DeleteMapping("/batch/{ids}")
    public Result batchDelete(@PathVariable("ids") List<Integer> ids){
        try{
            boolean delete = dictionaryService.batchDelete(ids);
            if (delete == true){
                return Result.success(200,"删除成功",null);
            }else {
                return Result.fail("删除失败，请稍后再试");
            }
        }catch (Exception e){
            return Result.fail("没有这个分类");
        }

    }
}
